from transformers import *
from PIL import Image,ImageDraw
#目标检测
pipe = pipeline(model="D:\models\owlvit-base-patch32",task="zero-shot-object-detection")
img = Image.open("D:\codes\sshcode\HuggingFace\img.png")
predictions = pipe(img,candidate_labels=["cat"])
print(predictions)
draw = ImageDraw.Draw(img)
for pre in predictions:
    box = pre['box']
    label = pre['label']
    score = pre['score']
    xmin, ymin, xmax, ymax = box.values()
    draw.rectangle((xmin, ymin, xmax, ymax), outline="red",width=2)
    draw.text((xmin, ymin), label + " " + str(round(score, 2)), fill="red")
img.show()